<template>
	<view class="center">
		<view class="logo" :hover-class="!islogin ? 'logo-hover' : ''">
			<image class="logo-img" :src="islogin ? uerInfo.avatarUrl :avatarUrl"></image>
			<view class="logo-title">
				<text class="uer-name">{{islogin ? uerInfo.nickName : '您未登录'}}</text>
			</view>
		</view>
		<view class="center-list">
			<view class="center-list-item border-bottom" @click="tomeetingorder">
				<text class="list-icon">&#xe60f;</text>
				<text class="list-text">我的会议报名</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<view class="center-list-item" @click="toketi">
				<text class="list-icon">&#xe639;</text>
				<text class="list-text">查询课题/征文状态</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
		</view>
		<view class="center-list">
			<view class="center-list-item border-bottom">
				<text class="list-icon">&#xe60b;</text>
				<text class="list-text">浏览历史</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<view class="center-list-item">
				<text class="list-icon">&#xe65f;</text>
				<text class="list-text">文章收藏</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
		</view>
		<view class="center-list">
			<view class="center-list-item" @click="tofontsize">
				<text class="list-icon">&#xe65f;</text>
				<text class="list-text">设置字体大小</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<view class="center-list-item" @click="toupdata">
				<text class="list-icon">&#xe614;</text>
				<text class="list-text">版本更新</text>
				<text class="navigat-arrow lastnav">v{{version}}</text>
			</view>
		</view>
		<view class="center-list" v-if="islogin">
			<view class="center-list-item" @click="layout">
				<text class="list-text layout">注销登录</text>
			</view>
		</view>
		<view class="wx" v-if="!islogin">
			<image class="wximg" src="../../static/weixin.png" @click="loginfn"></image>
		</view>
	</view>
</template>

<script>
	import {common} from '@/common/publish.js';
	export default {
		data() {
			return {
				login: false,
				avatarUrl: '/static/logo.png',
				version: '',
				islogin: false,
				uerInfo:{}
			}
		},
		onLoad() {
			var that = this;
			this.getver();
			//判断localstorge的login是否存在不存在/false则不处理，为true时自动登陆
			uni.getStorage({
				key: 'islogin',
				success: function (res) {
					if(res.data == true){
						that.islogin = res.data;
						
						uni.getStorage({
							key: 'uerInfo',
							success: function (res) {
								that.uerInfo = res.data;
							}
						});
						
					}
				}
			});
		},
		methods: {
			getver() {
				var that = this;
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					that.version = inf.version;
				});
			},
			tomeetingorder() {
				if(this.islogin){
					uni.navigateTo({
						url: '/pages/meetingorder/meetingorder'
					});
				}else{
					uni.showToast({
						title: '此模块需要登陆才能访问',
						icon:'none',
						duration: 1500
					});
				}
			},
			toketi() {
				uni.navigateTo({
					url: '/pages/webview/webview?url=http://nbnet.chinaecdc.com/api/index.htm'
				});
			},
			tofontsize() {
				uni.navigateTo({
					url: '/pages/fontsize/fontsize'
				});
			},
			layout(){
				uni.setStorage({
					key: 'islogin',
					data: false
				});
				this.islogin = false;
				uni.showToast({
					title: '注销成功',
					duration: 1500
				});
			},
			loginfn() {
				var that = this;
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									// 获取用户信息
									uni.getUserInfo({
										provider: 'weixin',
										success: function(infoRes) {
											that.uerInfo = infoRes.userInfo;
											uni.setStorage({
												key: 'uerInfo',
												data: that.uerInfo
											});
											uni.setStorage({
												key: 'islogin',
												data: true
											});
											
											uni.request({
												url: common.localurl+'/app/user/login.json' , //仅为示例，并非真实接口地址。
												data: {
													unionid: infoRes.userInfo.unionid
												},
												success: (res) => {
													that.islogin = true;
												}
											});
											
										},
										complete:(infoRes)=>{
											//console.log(JSON.stringify(infoRes))
										}
									});
								}
							});
						}
					}
				});
			},
			toupdata() {
				var that = this;
				plus.nativeUI.showWaiting("检测更新...")
				//首先判断版本
				var checkurl = common.localurl+'/api/edition/list.json';
				uni.request({
					url: checkurl,
					method: 'GET',
					success: (res) => {
						plus.nativeUI.closeWaiting();
						var newversion = res.data.data.editionInformation;
						var downloadurl = common.localurl+ + res.data.data.editionLink;
						if (that.version && newversion && (Number(that.version) < Number(newversion))) {
							plus.nativeUI.confirm("请确认是否更新版本？", function(e) {
								if (e.index == 1) {
									that.downWgt(downloadurl);
								} else {}
							}, "有新版本需要更新", ["取消", "升级"]);
						} else {
							plus.nativeUI.toast("无是最新版本!");
						}
					}
				});
			},
			//下载
			downWgt(downloadurl) {
				var that = this;
				plus.nativeUI.showWaiting("下载中...")
				uni.downloadFile({
					url: downloadurl, 
					success: (res) => {
						if (res.statusCode == 200) {
							plus.nativeUI.showWaiting("安装中...");
							that.installWgt(res.tempFilePath); // 安装wgt包
						} else {
							plus.nativeUI.toast("下载失败！");
						}
						plus.nativeUI.closeWaiting();
					},
					complete: (res) => {
						console.log(res.statusCode);
						console.log(res.tempFilePath);
					}
				});
			},
			//安装
			installWgt(path) {
				plus.nativeUI.showWaiting("安装中...");
				plus.runtime.install(path, {
					force: false
				}, function() {
					plus.nativeUI.closeWaiting();
					plus.runtime.restart();
				}, function(e) {
					plus.nativeUI.closeWaiting();
					plus.nativeUI.toast("安装中[" + e.code + "]：" + e.message);
				});
			}
		}
	}
</script>

<style lang="scss">
	@import "@/common/publish.scss";

	@font-face {
		font-family: texticons;
		font-weight: normal;
		font-style: normal;
		src: url('https://at.alicdn.com/t/font_984210_5cs13ndgqsn.ttf') format('truetype');
	}

	page,
	view {
		display: flex;
	}

	page {
		background-color: $bgcolor;
	}

	.center {
		flex-direction: column;
	}

	.logo {
		width: 750upx;
		height: 240upx;
		padding: 20upx;
		box-sizing: border-box;
		background-color: $fontblue;
		flex-direction: row;
		align-items: center;
	}

	.logo-hover {
		opacity: 0.8;
	}

	.logo-img {
		width: 150upx;
		height: 150upx;
		border-radius: 150upx;
	}

	.logo-title {
		height: 150upx;
		flex: 1;
		align-items: center;
		justify-content: space-between;
		flex-direction: row;
		margin-left: 20upx;
	}

	.uer-name {
		height: 60upx;
		line-height: 60upx;
		font-size: 38upx;
		color: #FFFFFF;
	}

	.go-login.navigat-arrow {
		font-size: 38upx;
		color: #FFFFFF;
	}

	.login-title {
		height: 150upx;
		align-items: self-start;
		justify-content: center;
		flex-direction: column;
		margin-left: 20upx;
	}

	.center-list {
		background-color: #FFFFFF;
		margin-top: 20upx;
		width: 750upx;
		flex-direction: column;
	}

	.center-list-item {
		height: 90upx;
		width: 750upx;
		box-sizing: border-box;
		flex-direction: row;
		padding: 0upx 20upx;
	}

	.border-bottom {
		border-bottom-width: 1upx;
		border-color: #c8c7cc;
		border-bottom-style: solid;
	}

	.list-icon {
		width: 40upx;
		height: 90upx;
		line-height: 90upx;
		font-size: 34upx;
		color: $fontblue;
		text-align: center;
		font-family: texticons;
		margin-right: 20upx;
	}

	.list-text {
		height: 90upx;
		line-height: 90upx;
		font-size: 34upx;
		color: #555;
		flex: 1;
		text-align: left;
	}

	.navigat-arrow {
		height: 90upx;
		width: 40upx;
		line-height: 90upx;
		font-size: 34upx;
		color: #555;
		text-align: right;
		font-family: texticons;
	}

	.lastnav {
		margin-right: 40upx;
	}

	.wx {
		text-align: center;
		margin-top: 80upx;
	}

	.wximg {
		width: 110upx;
		height: 110upx;
		margin: 0 auto;
	}

	.layout {
		text-align: center;
	}
</style>
